import { Models } from '../../../../common/typings/model'
import KoaRouter from 'koa-router'
import Mysql from '../../../../core/Mysql'
import { Success } from '../../../../core/HttpException'
import validator from '../../../../middlewares/validator'
import Auth from '../../../../middlewares/Auth'
import addMenu from '../../../../common/apiValidate/system/menu/add'
import DateUtils from '../../../../common/utils/DateUtils'
import Config from '../../../../config/Config'

const router = new KoaRouter({
  prefix: `${Config.api_prefix}system/menu`,
})

router.post('/add', Auth.verifyTokenPermission, validator(addMenu, 'body'), async (ctx: Models.Ctx) => {
  const { type, name, parentId, path = '', icon, serialNum, show, component = '', permission } = ctx.request.body
  const date = DateUtils.format(new Date())
  await Mysql.command(`
    INSERT INTO menu ( label, parent_id, path, icon, type, serial_num, \`show\`, component, permission,  created_at, updated_at )
    VALUES
    ( '${name}', ${parentId}, '${path}', '${icon}', ${type}, ${serialNum}, ${show}, '${component}', '${permission}', '${date}', '${date}' );
  `)
  // switch (type) {
  //   // 新建目录和菜单
  //   case 0:
  //   case 1: {
  //     await Mysql.command(`
  //       INSERT INTO menu ( label, parent_id, path, icon, serial_num, \`show\`, component,  created_at, updated_at )
  //       VALUES
  //       ( '${name}', ${parentId}, '${path}', '${icon}', ${serialNum}, ${show}, '${component}', '${date}', '${date}' );
  //     `)
  //     break
  //   }
  //   case 2: {
  //     await Mysql.command(`
  //       UPDATE
  //         menu
  //       SET actions = '${actions}'
  //       WHERE id = ${parentId}
  //     `)
  //     break
  //   }
  //   default: {
  //     throw new ParameterException('没有相关处理函数')
  //   }
  // }
  Auth.updateRedisRole()
  throw new Success()
})

export default router
